<?php


class Ext_Model_Lov_Row extends Ext_Db_Table_Row {

    protected $_lovType;

    public function __construct( array $config = array() ) {
        $table = $config[ 'table' ];

        $lovType = $table->getType();
        $this->_lovType = $lovType;
        if ( empty( $config['data']['type'] ) ) {
            $config[ 'data' ][ 'type' ] = $lovType;
        }
        $config['data']['id'] = $config['data'][ $table->getIdcol() ];
        return parent::__construct( $config );
    }

}

class Ext_Model_Lov extends Ext_Db_Table {

    // Este row le agrega un campo TYPE a los tables que heredan de esta clase.
    protected $_rowClass = "Ext_Model_Lov_Row";

    protected $_name = 'sys_lov';

    protected $_idCol = 'text_id';

    protected $_sequence = false;

    public function __construct( $config = array() )
    {        
        if (!is_array($config)) {
            $config = array(self::ADAPTER => $config);
        }
        $config[ self::PRIMARY ] = array( 'type', $this->_idCol );
        parent::__construct( $config );
    }


    public function getIdcol() {
        return $this->_idCol;
    }
    public function getType() {
        $e = explode( "_", get_class( $this ) );
        return $e[ count( $e ) - 1 ];
    }

    public function insert( array $data ) {
        $data[ 'type' ] = $this->getType();
        $data[ $this->getIdcol() ] = $data['id'];
        unset( $data[ 'id' ] );
        return parent::insert( $data );
    }

    public function find() {
        $args = func_get_args();
        $args = array_merge( array(
                        0 => $this->getType(),
                        $args
        ) );

        return call_user_func_array( array(
                        $this,
                        'parent::find'
        ), $args );
    }

    public function fetchAll( $where = null, $order = null, $count = null, $offset = null ) {

        if ( ! ( $where instanceof Zend_Db_Table_Select ) ) {
            $select = $this->select();

            if ( $where !== null ) {
                $this->_where( $select, $where );
            }

            if ( $order !== null ) {
                $this->_order( $select, $order );
            }

            if ( $count !== null || $offset !== null ) {
                $select->limit( $count, $offset );
            }

        } else {
            $select = $where;
        }
        $select->where( 'type = ?', $this->getType() );
        return parent::fetchAll( $select, $order, $count, $offset );
    }

    public function getValidator() {
        return new Ext_Validate_Lov( $this->info("name"), $this->getIdcol(), array( "field" => "type", "value" => $this->getType() ) );
    }

    public function getLov( $where = null, $order = null, $count = null, $offset = null ) {

        $select = $this->select();

        $select->from( 'sys_lov', array(
                        $this->getIdcol(),
                        'description'
        ) );
        if ( $where !== null ) {
            $this->_where( $select, $where );
        }

        if ( $order !== null ) {
            $this->_order( $select, $order );
        }

        if ( $count !== null || $offset !== null ) {
            $select->limit( $count, $offset );
        }
        $rowSet = $this->fetchAll( $select );
        $ret = array();
        foreach ( $rowSet as $row ) {
            $ret[ $row->{ $this->getIdcol() } ] = $row->description;
        }
        return $ret;
    }

    public function getNextId() {
        $type = $this->getType();

        $select = $this->getAdapter()->select()->from( array( "t" => $this->_name ) ,
                                            array( "r" => new Zend_Db_Expr( "max(" .$this->_idCol. ")" ) ));
        $select-> where( 'type = ?', $type );
        $ret = $select->query()->fetchColumn();
        if ( is_null( $ret ) ) {
            return 0;
        }
        return $ret + 1;
    }
}